package com.pb.infra.auth.controller;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.entity.excel.Column;
import com.pb.infra.oth.entity.excel.Excel;
import com.pb.infra.oth.entity.excel.ExcelSheet;
import com.pb.infra.auth.entity.pbsys.PriviUser;
import com.pb.infra.auth.service.PriviUserService;
import com.pb.infra.oth.exception.AppAuthException;
import com.pb.infra.oth.exception.ErrorCode;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.ExportExcelUtil;
import com.pb.infra.oth.util.StringUtil;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 *
 * @author liudebao
 * @date 2020/1/1
 */
@Slf4j
@RestController
@RequestMapping("v1/system/user")
public class PriviUserController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private PriviUserService userService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "privi_user";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "用户管理";

    /**
     * 查询用户信息
     *
     * @param condition 请求参数
     * @param page
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> condition, Page page, HttpServletRequest request) {
        return userService.select(condition, page, request);
    }

    /**
     * 新增用户信息
     *
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        Map<String, Object> map1 = userService.insert(map, request);
        if (map1 != null && !Constant.OK.equals(map1.get("code").toString()) && !StringUtil.isEmpty(map1.get("msg"))) {
            return Result.error(map1.get("code").toString(), map1.get("msg").toString());
        } else {
            return Result.ok(map1);
        }
    }

    /**
     * 修改用户信息
     *
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        return userService.update(map, request);
    }

    /**
     * 删除用户信息
     *
     * @param user 用户实体类
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result delete(@RequestBody PriviUser user) {
        return userService.delete(user);
    }

    /**
     *查询岗位
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selPost")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selPost(@RequestBody Map<String, Object> map) {
        return userService.selPost(map);
    }

    /**
     * 查询条线
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selectLine")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectLine(@RequestBody Map<String, Object> map) {
        return userService.selectLine(map);
    }

    /**
     * 查询用户职位
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selectPositionList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectPositionList(@RequestBody Map<String, Object> map) {
        return userService.selectPositionList(map);
    }

    /**
     * 新增职位信息
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insertPosition")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insertPosition(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        Map<String, Object> map1 = userService.insertPosition(map, request);
        if (map1 != null && !Constant.OK.equals(map1.get("code").toString()) && !StringUtil.isEmpty(map1.get("msg"))) {
            return Result.error(map1.get("code").toString(), map1.get("msg").toString());
        } else {
            return Result.ok(map1);
        }
    }

    /**
     *  修改职位信息
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("updatePosition")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result updatePosition(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        Map<String, Object> map1 = userService.updatePosition(map, request);
        if (map1 != null && !Constant.OK.equals(map1.get("code").toString()) && !StringUtil.isEmpty(map1.get("msg"))) {
            return Result.error(map1.get("code").toString(), map1.get("msg").toString());
        } else {
            return Result.ok(map1);
        }
    }

    /**
     *  查询密码策略
     * @param map 请求参数
     * @return Result 返回数据
     */
    @GetMapping("selPwdPolicy")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selPwdPolicy(@RequestParam Map<String, Object> map) {
        return userService.selPwdPolicy(map);
    }

    /**
     * 删除职位信息
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("deletePosition")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result deletePosition(@RequestBody Map<String, Object> map) {
        userService.deletePosition(map);
        return Result.OK_EMPTY;
    }

    /**
     * 恢复职位
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("changePosition")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result changePosition(@RequestBody Map<String, Object> map) {
        return userService.changePosition(map);
    }

    /**
     * 导出用户管理EXCEL
     *
     * @param response 请求返回对象
     * @throws Exception
     */
    @PostMapping("exportUserExcel")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.EXP, menuName = OPERATION_MENU)
    public void exportIndexSubqueryExcel(@RequestBody Map<String, Object> map, HttpServletResponse response, HttpServletRequest request) throws Exception {
        ExcelSheet sheet = new ExcelSheet();
        if (StringUtil.isEmpty(map.get("loginId"))) {
            throw new AppAuthException(ErrorCode.ErrorCodeEnum.ACCESS_USER_INFO);
        }
        if (StringUtil.isEmpty(map.get("manageFlag"))) {
            throw new AppAuthException(ErrorCode.ErrorCodeEnum.ACCESS_USER_INFO);
        }
        if (Constant.SYSTEM_ADMIN.equals(map.get("loginId").toString()) || Constant.EXIST.equals(map.get("manageFlag").toString())) {
            sheet.setHeader(new String[][]{{"法人", "用户号", "姓名", "主职位", "职位", "所属组织", "角色", "用户状态", "手机号", "邮箱", "密码策略", "密码过期时间", "创建时间"}});
            sheet.setColumn(new Column[]{new Column("corpName"), new Column("userId"), new Column("userName"), new Column("mainName"), new Column("positionName"), new Column("structName"), new Column("roleName"), new Column("statusName"), new Column("mobileNo"), new Column("email"), new Column("pwdPolicyName"), new Column("pwdExpiredTs"), new Column("createdTs")});
        } else {
            sheet.setHeader(new String[][]{{"用户号", "姓名", "主职位", "职位", "所属组织", "角色", "用户状态", "手机号", "邮箱", "密码策略", "密码过期时间", "创建时间"}});
            sheet.setColumn(new Column[]{new Column("userId"), new Column("userName"), new Column("mainName"), new Column("positionName"), new Column("structName"), new Column("roleName"), new Column("statusName"), new Column("mobileNo"), new Column("email"), new Column("pwdPolicyName"), new Column("pwdExpiredTs"), new Column("createdTs")});
        }
        List<Map<String, Object>> dataList = userService.selAllUser(map, request);
        sheet.setData(dataList);
        ExportExcelUtil.exportExcel2007(new Excel("用户信息"), new ExcelSheet[]{sheet}, response);
    }

    /**
     * 查询用户所有职位
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selectAllPosition")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectAllPosition(@RequestBody Map<String, Object> map) {
        return userService.selectAllPosition(map);
    }

    /**
     *重置用户密码
     * @param userList 请求参数
     * @return Result 返回数据
     */
    @PostMapping("resetPasswd")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result resetPasswd(@RequestBody List<String> userList) {
        userService.resetPasswd(userList);
        return Result.OK_EMPTY;
    }

}
